package com.qijian.maindata.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qijian.common.annotation.Log;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.core.page.TableDataInfo;
import com.qijian.common.enums.BusinessType;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.qijian.maindata.domain.InspectionItem;
import com.qijian.maindata.domain.InspectionItemDetail;
import com.qijian.maindata.domain.query.InspectionItemQuery;
import com.qijian.maindata.domain.vo.InspectionItemVo;
import com.qijian.maindata.service.IInspectionItemDetailService;
import com.qijian.maindata.service.IInspectionItemService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 检验项-新Controller
 *
 * @author qijian
 * @date 2024-02-02
 */
@Api(tags = "检验项-新")
@RestController
@RequiredArgsConstructor
@RequestMapping("/maindata/schemeItem")
public class InspectionSchemeItemController extends BaseController {


    private final IInspectionItemService inspectionItemService;

    private final IInspectionItemDetailService inspectionItemDetailService;

    /***
     * 检验项列表查询
     * @param query
     * @param page
     * @return
     */
    @PreAuthorize("@ss.hasPermi('maindata:details:list')")
    @GetMapping("/list")
    public TableDataInfo list(InspectionItemQuery query, IPage<InspectionItem> page) {
//        initOrderItem(InspectionItem.class, page.orders());
        return getDataTable(inspectionItemService.page(Page.of(page.getCurrent(), page.getSize()), WhereEntityTool.invoke(query)));
    }

    /***
     * 列表出检验项(带检验明细)
     * @param query
     * @param page
     * @return
     */
    @GetMapping("/listVo")
    public TableDataInfo listVo(InspectionItemQuery query, IPage<InspectionItem> page){
        IPage<InspectionItem> pageResult = inspectionItemService.page(Page.of(page.getCurrent(), page.getSize()), WhereEntityTool.invoke(query));
        if(CollUtil.isNotEmpty(pageResult.getRecords())){
            pageResult.getRecords().forEach(p->{
                p.setDetails(inspectionItemDetailService.list(new LambdaQueryWrapper<InspectionItemDetail>().eq(InspectionItemDetail::getMainItemId,p.getId())));
            });
        }
        return getDataTable(pageResult);
    }

    /**
     * 新增检验项
     */
    @PreAuthorize("@ss.hasPermi('maindata:details:add')")
    @Log(title = "新增检验项-新", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InspectionItemVo inspectionItemVo) {
        return toAjax(inspectionItemService.saveItemVo(inspectionItemVo));
    }

    /**
     * 新增检验项
     */
    @PreAuthorize("@ss.hasPermi('maindata:details:edit')")
    @Log(title = "修改检验项-新", businessType = BusinessType.INSERT)
    @PutMapping
    public AjaxResult edit(@RequestBody InspectionItemVo inspectionItemVo) {
        return toAjax(inspectionItemService.saveItemVo(inspectionItemVo));
    }

    /***
     * 根据id获取明细
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('maindata:details:query')")
    @GetMapping("/{id}")
    public AjaxResult getInfo(@PathVariable Long id){
        return AjaxResult.success(inspectionItemService.getInfo(id));
    }


    /***
     * 删除检验项
     * @param mainIds
     * @return
     */
    @PreAuthorize("@ss.hasPermi('maindata:details:remove')")
    @Log(title = "检验项目删除主表-新", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteMains")
    public AjaxResult deleteMain(@RequestBody List<Long> mainIds){
        return AjaxResult.success(inspectionItemService.deleteMains(mainIds));
    }
}
